<?php

	use backend\models\TmeOrderinfo;
	use backend\models\TmeOrderdetailsinfo;
	use backend\models\TmeMerchandiseinfo;
	use backend\models\TmeUnitinfo;
	use backend\models\TbaMemberinfo;

//var_dump($_POST);exit;
if(\yii::$app->request->isPost){
   
	$start=intval(\yii::$app->request->post('start'));    //起始数据条数
	$length=intval(\yii::$app->request->post('length'));  //需要显示的数据条数
	$draw=intval(\yii::$app->request->post('draw'));      
	
	$column=intval(\yii::$app->request->post('order')[0]['column']);   //排序的列
	$dir = \yii::$app->request->post('order')[0]['dir'];     //排序方式
	
	$columArry=[
		"0"=>"ID",
		"1"=>"BillCode",
		"2"=>"RecMan",
		"3"=>"DeliveryName",
		"4"=>"TotalMoney",
		"5"=>"BillStatus",
		"6"=>"OrderTime",
		
	];
	
	
	//构建排序条件
	if($column == 0){
		$cloumName=$columArry[0];
	}else{	
		$cloumName=$columArry[$column];
	}
	
	
	$state = \yii::$app->request->post('state');         //订单状态
	if( $state !== "all"){
		
		$state = \yii::$app->request->post('state');         //订单状态
		$states = " BillStatus='".$state."' and";
		
	}else{
		$states = "";
	}
	
	
	//根据搜索值时构建查询条件
	if(\yii::$app->request->post('search')['value']!=""){
		
		$value=\yii::$app->request->post('search')['value'];
		if(is_string($value))
			$where="$states  BillCode like '%".$value."%'  or $states  RecMan like '%".$value."%'  or $states  DeliveryName like '%".$value."%'";
		if(is_numeric($value))
		$where = " $states  BillCode like '%".$value."%' or $states  TotalMoney like '%".$value."%' or $states  OrderTime like '%".$value."%' ";
		
	}else{
		
		$state = \yii::$app->request->post('state');         //订单状态
		//订单状态
		if( $state !== "all"){
			
			$state = \yii::$app->request->post('state');     //订单状态
			$states = " BillStatus='".$state."'";			 //订单状态为枚举类型查找时必须带上引号
			$where=$states;
		}else{
			$states = "";
			$where="";
		}
		
	}
	
	//echo $where;
	
	//根据条件查询数据
    $order = TmeOrderinfo::find()->where($where)->orderBy($cloumName." ".$dir)->limit($length)->offset($start)->all(); 
    $total =TmeOrderinfo::find()->where($where)->orderBy($cloumName." ".$dir)->count();        //查询数据总数
    $arr=[];
    foreach ($order as $v){
    	  		
   		//判断订单状态
   		if($v['BillStatus'] == 0 ){
   			$state = "<span class='label label-sm label-warning'>待审核</span>";
   			$examine = "<div class='btn btn-xs btn-success' onclick='return examine(".$v['ID'].")'  >审核</div>";
   		}	
   		if($v['BillStatus'] == 1 ){
   			$state = "<span class='label label-sm label-warning2'>待发货</span>";
   			$examine ="";
   		}
   		if($v['BillStatus'] == 2 ){
   			$state = "<span class='label label-sm label-warning2'>已发货</span>";
   			$examine ="";
   		}
   		if($v['BillStatus'] == 3 ){
   			$state = "<span class='label label-sm label-warning2'>已收货</span>";
   			$examine ="";
   		}
    	$temp=[];
    	$temp['check']=" <label class=\"pos-rel\">															
 							<input type='checkbox' name='check[]'  value=".$v['ID']."  class='ace'>
 							<span class='lbl'></span>
 					   </label>";
    	$temp['BillCode']=$v['BillCode'];
    	$temp['RecMan']=$v['RecMan'];
    	$temp['Delivery']=$v['DeliveryName'];
    	$temp['TotalMoney']=$v['TotalMoney'];
    	$temp['BillStatus']= $state; 

    	$temp['OrderTime']=$v['OrderTime'];
    	$temp['options']="<center><div class='hidden-sm hidden-xs action-buttons'>
								<a class='blue' href='#' onclick='return see(".$v['ID'].")'>
									<i class='ace-icon fa fa-search-plus bigger-130'></i>   							
								</a>

								<a class='green' href='#' onclick='return alter(".$v['ID'].")' >
									<i class='ace-icon fa fa-pencil bigger-130'></i>
								</a>

								<a class='red' href='#' onclick='return delet(".$v['ID'].")' >
									<i class='ace-icon fa fa-trash-o bigger-130'></i>
								</a>".$examine."
							</div></center>";
    	 	
  	 	$arr[]=$temp;
  	 	   
    }
	    
    //构造返回数据
    $tmp=[
 	   "draw"=>$draw,
 	   "recordsTotal"=>$total,     //数据总数
 	   "recordsFiltered"=>$total,  //数据总数
 	   "data"=>$arr                //数据
   
    ];
   
    echo  json_encode($tmp);  //将数据转变为json字符串

}

?>